package com.lj.audiorecorderapp;

import com.lj.audiorecorderapp.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;

/**
 * ex.
 *
 * @author lj
 * @since 2021-05-12
 */
public class MainAbility extends Ability {
    private static final int MPERMISSIONREQCODE = 100;
    private final String[] mNeedPermissions = {
        "ohos.permission.MICROPHONE"
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());
        if (verifyCallingPermission(mNeedPermissions[0]) != IBundleManager.PERMISSION_GRANTED) {
            requestPermissionsFromUser(mNeedPermissions, MPERMISSIONREQCODE);
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (requestCode == MPERMISSIONREQCODE) {
            if (grantResults[0] != IBundleManager.PERMISSION_GRANTED) {
                terminateAbility();
            }
        } else {
            terminateAbility();
        }
    }
}
